<style lang="less">
  @import './login.less';
</style>

<template>
  <div class="login" @keydown.enter="handleSubmit">
    <div class="login-con">
      <Card :bordered="false">
        <p slot="title">
          <Icon type="log-in"></Icon>
          欢迎登录
        </p>
        <div class="form-con">
          <Form ref="loginForm" :model="form" :rules="rules">
            <FormItem prop="userName">
              <Input v-model="form.userName" placeholder="请输入用户名">
              <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
              </Input>
            </FormItem>
            <FormItem prop="password">
              <Input type="password" v-model="form.password" placeholder="请输入密码">
              <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
              </Input>
            </FormItem>
            <FormItem>
              <Button @click="handleSubmit" type="primary" long :loading="actionLoading">登录</Button>
            </FormItem>
          </Form>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
  import Cookies from 'js-cookie';
  import service from './service';

  export default {
    data () {
      return {
        form: {
          userName: '',
          password: ''
        },
        rules: {
          userName: [
            {required: true, message: '账号不能为空', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '密码不能为空', trigger: 'blur'}
          ]
        }
      };
    },
    computed: {
      actionLoading () {
        return this.$store.state.app.actionLoading;
      }
    },
    methods: {
      handleSubmit () {
        this.$refs.loginForm.validate((valid) => {
          if (valid) {
            this._hyTool.cal(true);
            let loginInfo = {};
            loginInfo.username = this.form.userName;
            // todo 登录密码加密
            loginInfo.password = this.form.password;
            Cookies.set('user', this.form.userName);
            Cookies.set('password', this.form.password);
            Cookies.set('buttonList', ',' + 'home,info,carBaseInfo,carAdd,fullChartMonitoring,userManage,user,role,org,position,realTime,history,historyTrack,userAdd,userEdit,userLock,roleAdd,roleEdit,roleAuth,roleDelete' + ',');
            this.$router.push('/home');
            try {
              service.loginSave(loginInfo).then((result) => {
                this._hyTool.cal(false);
                Cookies.set('userId', this.form.userId);
                Cookies.set('user', this.form.userName);
                Cookies.set('password', this.form.password);
                // 保存token
                this._hyTool.setStore('access_token', result.data);
                // 获取用户信息
                service.getCurrentUser().then((result) => {
                  // 菜单ID
                  Cookies.set('userInfo', result.data);
                  // this._hyTool.setStore('buttonList', ',home,userManage,user,role,org,position,realTime,history,historyTrack,userAdd,userEdit,userLock,roleAdd,roleEdit,roleAuth,roleDelete' + ',');
                  // Cookies.set('buttonList', ',home,');
                  Cookies.set('buttonList', ',home,' + result.data.buttons + ',');
                  this.$router.push('/home');
                });
              });
            } catch (e) {
              this._hyTool.cal(false);
              this.modal_loading = false;
            }
          }
        });
      }
    }
  };
</script>
